package db10;

import java.io.*;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class StudentSystem {
    static Map<Integer, Student> studentMap = new HashMap<>();
    static Map<String, Student> studentMap1 = new HashMap<>();
    static {
        studentMap.put(1,new Student(1,"zs",18,"男"));
        studentMap.put(2,new Student(2,"ls",18,"男"));
    }
    static Scanner input = new Scanner(System.in);

    public static void main(String[] args) {
        while (true) {
            System.out.println("1.查看所有学生信息");
            System.out.println("2.输入姓名搜索学生信息");
            System.out.println("3.增加学生信息");
            System.out.println("4.修改学生信息");
            System.out.println("5.删除学生信息");
            System.out.println("6.退出");
            System.out.println("请输入编号选择功能");
            int index = input.nextInt();
            if (index == 1) {
                selectAll();
            } else if (index == 2) {
                selectByName();
            } else if (index == 3) {
                add();
            } else if (index == 4) {

            } else if (index == 5) {

            } else if (index == 6) {
                break;
            } else {
                System.err.println("没有此功能");
            }
        }
    }

    /**
     *
     */
    public static void selectAll() {
        // 输出所有学生信息
        if (studentMap.size() == 0) {
            System.err.println("没有学生信息");
        } else {
            Collection<Student> values = studentMap.values();
            for (Student value : values) {
                System.out.println(value);
            }
        }
    }

    public static void selectByName() {
        System.out.println("请输入要查询的学生姓名");
        String name = input.next();

        Student student = studentMap.get(name);

        if (student == null) {
            System.err.println("没有此学生");
        } else {
            System.out.println(student);
        }
    }

    public static void add() {
        // 1.创建学生对象,但是此时学生对象里的属性都是默认值
        Student student = new Student();

        // 2.通过录入器给学生的属性录入值
        System.out.println("请输入学生编号");
        student.setNo(input.nextInt());

        System.out.println("请输入姓名");
        student.setName(input.next());

        System.out.println("请输入年龄");
        student.setAge(input.nextInt());

        System.out.println("请输入身高");
        student.setHeight(input.nextDouble());

        System.out.println("请输入性别");
        student.setSex(input.next());

        studentMap1.put((student.getName()),student);

        save();
    }

    public static void save() {
        // 将map集合的数据保存到文件中
        try {
            OutputStream outputStream = new FileOutputStream("D:/IDEA/data.data");
            Collection<Student> values = studentMap.values();
            for (Student student : values) {
                outputStream.write(student.format().getBytes("utf-8"));
            }
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
